package com.example.firstjavamqtt.factory.strategy.callback;

import com.example.firstjavamqtt.model.base.Device;
import com.example.firstjavamqtt.service.base.DeviceService;
import com.example.firstjavamqtt.service.schedule.SpringScheduledTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class HandleHeartMsgStrategy implements HandleMessageStrategy{

    static Map<String, Date> map = new HashMap<>();

    private static SpringScheduledTaskService springScheduledTaskService;

    private static DeviceService deviceService;


    @Override
    public void handle(String msg) {
        //设置心跳设备的id和时间
        map.put(msg, new Date());
        //map存入定时任务
        springScheduledTaskService.setMap(map);
    }

    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        HandleHeartMsgStrategy.deviceService = deviceService;
        List<Device> deviceList = deviceService.getDeviceList();
        deviceList.forEach(k -> {
            map.put(k.getDeviceId(), k.getLastOnlineDate());
        });
        System.out.println("成功初始化map");
    }

    @Autowired
    public void setSpringScheduledTaskService(SpringScheduledTaskService springScheduledTaskService) {
        HandleHeartMsgStrategy.springScheduledTaskService = springScheduledTaskService;
    }
}
